package com.admin.service.impl;

import com.admin.entity.Admin;
import com.admin.mapper.AdminMapper;
import com.admin.service.IAdminshuchaxunService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@SuppressWarnings("ALL")
@Service
public class AdminshuchaxunImpl implements IAdminshuchaxunService {
    @Autowired
    private AdminMapper adminMapper;
    /**
     * 根据id查询自己和下级的数据，
     * 只有admin_id,admin_name,admin_parent三个
     * json型
     * @param id 用户名
     * @return
     */
    @Override
    public String getTreeByUserId(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("admin_phone",id);
        Admin admin = adminMapper.selectOne(queryWrapper);
        String ids = adminMapper.getAllChildUser(admin.getAdminId());
        List<Admin> list = adminMapper.getxiajiyonghubyid(ids);
        StringBuffer sb = new StringBuffer("[");
        if (list.size()>0)
        {
            for (Admin a : list) {
                int userID = a.getAdminId();
                //adminService.countPid(userID);
                QueryWrapper q = new QueryWrapper();
                q.eq("admin_parent", userID);
                int count =  adminMapper.selectCount(q);
                String shuju = "{id:" + a.getAdminId() + ",pid:" + a.getAdminParent() + ",name:\"" + a.getAdminName() + "\",isParent:\"" + (count>0) + "\"},";
                sb.append(shuju);
            }
            sb.setCharAt(sb.length()-1, ']');
        }
        else
        {
            sb.append("]");
        }
        return sb.toString();
    }






}
